<template>
  <div class="app-container">

    <el-card style="border-radius: 10px">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="单据类型" prop="sourceDocType">
          <el-select v-model="queryParams.sourceDocType" placeholder="选择单据类型" clearable>
            <el-option
              v-for="dict in dict.type.source_doc_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="物料编码" prop="itemCode">
          <el-input
            v-model="queryParams.itemCode"
            placeholder="请输入物料编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="itemName">
          <el-input
            v-model="queryParams.itemName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="批次码" prop="batchCode">
          <el-input
            v-model="queryParams.batchCode"
            placeholder="请输入批次码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="入库仓库" prop="warehouseId">
          <el-cascader
            v-model="warehouseInfo"
            :options="warehouseOptions"
            :props="warehouseProps"
            @change="handleWarehouseChanged"
            ref="cascaderRef"
            clearable
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="操作日期" prop="params">
          <el-date-picker
            clearable
            v-model="queryParams.params"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            placeholder="请选择操作日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div style="height: 15px"></div>
    <el-card style="border-radius: 10px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['wm:transaction:statement:export']"
          >导出</el-button>
        </el-col>
      </el-row>

      <el-table
        v-loading="loading"
        :data="TransactionList"
        border
      >
        <el-table-column label="单据类型"  align="center" width="120px" prop="sourceDocType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.source_doc_type" :value="scope.row.sourceDocType"/>
          </template>
        </el-table-column>
        <el-table-column label="单据编码"  align="center" prop="sourceDocCode" width="120px" :show-overflow-tooltip="true"/>
        <el-table-column label="物料编码"  align="center" prop="itemCode" width="120px" :show-overflow-tooltip="true"/>
        <el-table-column label="物料名称"  align="center" prop="itemName" :show-overflow-tooltip="true"/>
        <el-table-column label="规格型号"  align="center" prop="specification" :show-overflow-tooltip="true"/>
        <el-table-column label="单位"  align="center" prop="unitOfMeasure" />
        <el-table-column label="批次码"  align="center" prop="batchCode" :show-overflow-tooltip="true"/>
        <el-table-column label="库区库位"  align="center" prop="warehouseName" :show-overflow-tooltip="true"/>
        <el-table-column label="往来单位名称" width="120" align="center" prop="vendorName" :show-overflow-tooltip="true"/>
        <el-table-column label="生产工单号" width="100" align="center" prop="workorderCode" :show-overflow-tooltip="true"/>
        <el-table-column label="操作日期"  align="center" prop="transactionDate" :show-overflow-tooltip="true"/>
        <el-table-column label="数量"  align="center" prop="transactionQuantity" >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.transactionQuantity >0" type="primary">{{scope.row.transactionQuantity }}</el-tag>
            <el-tag v-else type="danger">{{ -scope.row.transactionQuantity }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="单价" align="center" prop="price" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.price }}</span>
          </template>
        </el-table-column>
        <el-table-column label="金额" align="center" prop="billAmount" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.transactionQuantity >0" type="primary">{{scope.row.billAmount | formatMoney }}</el-tag>
            <el-tag v-else type="danger">{{ (-1 * scope.row.billAmount) | formatMoney }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作类型"  align="center" prop="transactionFlag">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.transactionFlag === -1" type="danger">出库</el-tag>
            <el-tag v-else-if="scope.row.transactionFlag === 1">入库</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { listTransaction } from "@/api/mes/wm/transaction";
import { getTreeList } from "@/api/mes/wm/warehouse"


export default {
  name: "TransactionStatement",
  dicts: ['transaction_type','source_doc_type'],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      TransactionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sourceDocType: null,
        itemCode: null,
        itemName: null,
        batchCode: null,
        warehouseId: null,
        locationId: null,
        areaId: null,
        params: [],
      },
      // 仓库下拉框数据
      warehouseOptions: [],
      // 表单仓库参数
      warehouseInfo:[],
      // 仓库树形参数
      warehouseProps:{
        checkStrictly: true,
        expandTrigger: 'hover',
        multiple: false,
        value: 'pId',
        label: 'pName',
      },
    };
  },
  created() {
    this.getList();
    this.getWarehouseList();
  },

  methods: {
    /** 查询产品销售退货行列表 */
    getList() {
      this.loading = true;
      const query = { ...this.queryParams };
      if (query.params && query.params.length === 2) {
        query.params = {
          beginTime: query.params[0],
          endTime: query.params[1]
        };
      } else {
        query.params = null;
      }
      listTransaction(query).then(response => {
        this.TransactionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 查询条件重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        sourceDocType: null,
        itemName: null,
        batchCode: null,
        warehouseId: null,
        locationId: null,
        areaId: null,
      }
      this.warehouseInfo = [];
      this.resetForm("queryForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('/mes/wm/transaction/export', {
        ...this.queryParams
      }, `出入库明细报表_${new Date().getTime()}.xlsx`)
    },
    // 查询仓库树形结构
    getWarehouseList(){
      getTreeList().then(response =>{
        if(response.data){
          this.warehouseOptions = response.data.filter((el) =>{
            return el.warehouseCode.indexOf('VIR') == -1;
          });;
        }
        this.warehouseOptions.map(w =>{
          w.children.map(l =>{
            let lstr =JSON.stringify(l.children).replace(/locationId/g,'lId').replace(/areaId/g, 'pId').replace(/areaName/g,'pName');
            l.children = JSON.parse(lstr);
          });

          let wstr = JSON.stringify(w.children).replace(/warehouseId/g,'wId').replace(/locationId/g, 'pId').replace(/locationName/g,'pName');
          w.children =  JSON.parse(wstr);

        });
        let ostr=JSON.stringify(this.warehouseOptions).replace(/warehouseId/g,'pId').replace(/warehouseName/g, 'pName');
        this.warehouseOptions = JSON.parse(ostr);
      });
    },
    //选择默认的仓库、库区、库位
    handleWarehouseChanged(obj){
      if(obj !=null){
        this.queryParams.warehouseId = obj[0];
        this.queryParams.locationId = obj[1];
        this.queryParams.areaId = obj[2];
      }
      this.$refs.cascaderRef.dropDownVisible = false;
      this.handleQuery();
    },

  }
  ,  filters: {
    formatMoney(value) {
      if (isNaN(value)) return '0.00';
      return Number(value)
        .toFixed(2)
        .replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    },
  }
};
</script>
<style>
.el-cascader-panel .el-radio {
  width: 100%;
  height: 100%;
  z-index: 10;
  position: absolute;
  top: 0;
  right: 10px;
}

.el-cascader-node__label {
  width: 157px;
}

.el-cascader-panel .el-radio__input {
  visibility: hidden;
}

.el-cascader-panel .el-cascader-node__postfix {
  top: 10px;
}
</style>
